package com.chuangke.framework.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chuangke.common.constant.ColumnConstants;
import com.chuangke.common.enums.OrderStatusEnum;
import com.chuangke.common.utils.SpringContextUtil;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;

public class OrderFlowUtilsV2 {

	/**
	 * 校验单据是否可以编辑
	 * 
	 * @param <T>
	 * @param servie
	 * @param orderIdList
	 * @return
	 */
	public static <T> boolean isEditable(IService<T> servie, List<String> orderIdList) {
		QueryWrapper<T> qw = new QueryWrapper<>();
		qw.ne(ColumnConstants.COL_STATUS, OrderStatusEnum.EDIT.getCode());
		qw.in(ColumnConstants.COL_BILL_ID, orderIdList);
		List<T> list = servie.list(qw);
		return CollectionUtils.isEmpty(list);
	}

	/**
	 * 获取不可编辑的单据
	 * 
	 * @param <T>
	 * @param servie
	 * @param orderIdList
	 * @return
	 */
	public static <T> List<T> getUnEditableData(IService<T> servie, List<String> orderIdList) {
		QueryWrapper<T> qw = new QueryWrapper<>();
		qw.ne(ColumnConstants.COL_STATUS, OrderStatusEnum.EDIT.getCode());
		qw.in(ColumnConstants.COL_BILL_ID, orderIdList);
		return servie.list(qw);
	}
	
	public static <T> void submitBatch(String daoServieBeanName, List<String> orderIdList) {
		IService service = (IService) SpringContextUtil.getBean(daoServieBeanName);
		submitBatch(service,orderIdList) ;
	}

	/**
	 * 批量提交
	 * 
	 * @param <T>
	 * @param servie
	 * @param orderIdList
	 */
	public static <T> void submitBatch(IService<T> servie, List<String> orderIdList) {
		UpdateWrapper<T> uw = new UpdateWrapper<>();
		uw.set(ColumnConstants.COL_STATUS, OrderStatusEnum.APPROVAL.getCode());
		uw.in(ColumnConstants.COL_BILL_ID, orderIdList).eq(ColumnConstants.COL_STATUS, OrderStatusEnum.EDIT.getCode());
		servie.update(uw);
	}

	public static <T> void cancelSubmitBatch(String daoServieBeanName, List<String> orderIdList) {
		IService service = (IService) SpringContextUtil.getBean(daoServieBeanName);
		cancelSubmitBatch(service,orderIdList) ;
	}
	
	/**
	 * 批量撤回
	 * 
	 * @param <T>
	 * @param servie
	 * @param orderIdList
	 */
	public static <T> void cancelSubmitBatch(IService<T> servie, List<String> orderIdList) {
		UpdateWrapper<T> uw = new UpdateWrapper<>();
		uw.set(ColumnConstants.COL_STATUS, OrderStatusEnum.EDIT.getCode());
		uw.in(ColumnConstants.COL_BILL_ID, orderIdList).eq(ColumnConstants.COL_STATUS, OrderStatusEnum.APPROVAL.getCode());
		servie.update(uw);
	}

	public static <T> void rejectBatch(String daoServieBeanName, List<String> orderIdList) {
		IService service = (IService) SpringContextUtil.getBean(daoServieBeanName);
		rejectBatch(service,orderIdList) ;
	}
	/**
	 * 批量退回
	 * 
	 * @param <T>
	 * @param servie
	 * @param orderIdList
	 */
	public static <T> void rejectBatch(IService<T> servie, List<String> orderIdList) {
		UpdateWrapper<T> uw = new UpdateWrapper<>();
		uw.set(ColumnConstants.COL_STATUS, OrderStatusEnum.EDIT.getCode());
		uw.in(ColumnConstants.COL_BILL_ID, orderIdList).eq(ColumnConstants.COL_STATUS, OrderStatusEnum.APPROVAL.getCode());
		servie.update(uw);
	}
	
	public static <T> void completeBatch(String daoServieBeanName, List<String> orderIdList) {
		IService service = (IService) SpringContextUtil.getBean(daoServieBeanName);
		completeBatch(service,orderIdList) ;
	}

	/**
	 * 批量完成
	 * 
	 * @param <T>
	 * @param servie
	 * @param orderIdList
	 */
	public static <T> void completeBatch(IService<T> servie, List<String> orderIdList) {
		UpdateWrapper<T> uw = new UpdateWrapper<>();
		uw.set(ColumnConstants.COL_STATUS, OrderStatusEnum.COMPLETE.getCode());
		uw.in(ColumnConstants.COL_BILL_ID, orderIdList).eq(ColumnConstants.COL_STATUS, OrderStatusEnum.APPROVAL.getCode());
		servie.update(uw);
	}
	
}
